<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Data Matakuliah',
				width: 500,
				height: 400,
				closed: true,
				cache: false,
				href: base_url+'matakuliah/add',
				modal: true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'matakuliah/save/add';
		}
		// end newData
		
		importDialog = function (){  
			$('#dlgimport_mk').dialog('open').dialog('setTitle','Import Data Matakuliah');
			$('#fmimport_mk').form('clear');  
			url = base_url+'matakuliah/import'; 
		}
		
		importData=function(){
			$('#fmimport_mk').form('submit',{
				url: url,
				onSubmit: function(){
					//return $(this).form('validate');
				},
				success: function(result){
					alert(result);
					var result = eval('('+result+')');
					if (result.success){
						 $.messager.show({
							title: 'Sukses',
							msg: result.msg
						}); 
						$('#dlgimport_mk').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');	// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		
		addSAP = function (val){
			$('#dialog-sap').dialog({
				title: 'Tambah/Edit Detail Matakuliah',
				width: 700,
				height: $(window).height() * 0.99,
				closed: true,
				cache: false,
				href: base_url+'matakuliah/addsap/'+val,
				modal: true
			});
			 
			$('#dialog-sap').dialog('open');
			url = base_url+'matakuliah/save_sap';
		}
		// end addSAP
		
		editData = function (val){
			$('#dialog').dialog({
				title: 'Edit Data Matakuliah',
				width: 500,
				height: 375,
				closed: true,
				cache: false,
				href: base_url+'matakuliah/edit/'+val,
				modal: true
			});
			
			$('#dialog').dialog('open');  
			url = base_url+'matakuliah/save/edit';
		}
		//end editData
		
		deleteData = function (val){
			if(confirm("Apakah yakin akan menghapus data '" + val + "'?")){
				var response = '';
				$.ajax({ type: "GET",
					 url: base_url+'matakuliah/delete/' + val,
					 async: false,
					 success : function(response){
						var response = eval('('+response+')');
						if (response.success){
							$.messager.show({
								title: 'Success',
								msg: 'Data Berhasil Dihapus'
							});
							// reload and close tab
							$('#dg').datagrid('reload');
						} else {
							$.messager.show({
								title: 'Error',
								msg: response.msg
							});
						}
					 }
				});
			}
		}
		//end deleteData 
		
		saveData = function(){
			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						$('#dialog').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');		// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		//end saveData
		
		// action
		actionbutton = function(value, row, index){
			var col;
			<?if($this->mdl_auth->CekAkses(array('menu_id'=>14, 'policy'=>'DETAIL'))){?>
			col = '<a href="#" onclick="addSAP(\''+row.id_matakuliah+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false" title="Tambah Detail MK">Detail</a>';
			<?}?>
			col += '&nbsp;&nbsp;|&nbsp;&nbsp;';
			<?if($this->mdl_auth->CekAkses(array('menu_id'=>14, 'policy'=>'EDIT'))){?>
			col += '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="editData(\''+row.id_matakuliah+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Edit</a>';
			<?}?>
			col += '&nbsp;&nbsp;|&nbsp;&nbsp;';
			<?if($this->mdl_auth->CekAkses(array('menu_id'=>14, 'policy'=>'DELETE'))){?>
			col += '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="deleteData(\''+row.id_matakuliah+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Delete</a>';
			<?}?>
			return col;
		}
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				kd_fakultas : $('#s_kd_fakultas').val(),
				kd_prodi : $('#s_kd_prodi').val(),
				kd_kurikulum : $('#s_kd_kurikulum').val()
			});
		}
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>matakuliah/grid"});
		});
		
		// expand all group
		expandAllGroup = function(data){
			//alert(JSON.stringify(data));
			for(var i=0; i<data.total; i++){
				$('#dg').datagrid('expandGroup', i);
			}
		}
		
		// onchange
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
		});
		$('#s_kd_prodi').change(function(){
			$('#s_kd_kurikulum').load(base_url+'prosedur/getKurikulumbyProdi/'+$('#s_kd_prodi').val());
		});
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					},
					{
						iconCls:'icon-print',
						text:'Export PDF',
						handler:function(){
						var kd_fakultas = $('#s_kd_fakultas').val();
						var kd_prodi = $('#s_kd_prodi').val();
						var kd_kurikulum = $('#s_kd_kurikulum').val();
						if(kd_prodi !='' && kd_kurikulum !=''){
							window.open('<?=base_url().'matakuliah/laporan_pdf'?>/'+$('#s_kd_prodi').val()+'/'+$('#s_kd_kurikulum').val());
						}
						else{
								$.messager.show({
									title: 'Warning',
									msg: 'Harap Isi Filter Terlabih Dahulu'
								});
							}
						}
					},
					{
						iconCls:'icon-excel',
						text:'Export EXCEL',
						handler:function(){
							window.open('<?=base_url().'matakuliah/laporan_excel'?>');
						}
					},
					{
						iconCls:'icon-upload',
						text:'Import EXCEL',
						handler:function(){
							importDialog();
							//window.open('<?=base_url().'jenjang/import'?>');
						}
					}	
				]
			});			
		});
		
		//filter import
		$('#kd_fakultas2').change(function(){
			$('#kd_prodi2').load(base_url+'prosedur/getProdibyFakultas/'+$('#kd_fakultas2').val());
		});
		
		//$('#kd_prodi2').change(function(){
		//	$('#kd_dosen').load(base_url+'prosedur/getDosenWalibyProdi/'+$('#kd_prodi').val());
		//});
		
	});
</script>
<table id="dg" title="Kelola Data Matakuliah" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:50,
			pageList:[10,20,30,40,50,100,150,200],
			fit:true,
			toolbar:'#toolbar',
			view:groupview,
			groupField:'semester',
			groupFormatter:function(value, rows){
				return 'Semester ' + value;
			},
			onLoadSuccess: expandAllGroup
			">
	<thead>
		<tr>
			<th field="id_matakuliah" hidden="true" width="80"></th>
			<th field="kd_fakultas" hidden="true" width="80"></th>
			<th field="kd_prodi" hidden="true" width="80"></th>
			<th field="kd_matakuliah" width="100">Kode MK</th>
			<th field="nama_matakuliah" width="300">Nama Mata Kuliah</th>
			<th field="sks" width="75">SKS</th>
			<th field="semester" width="75">Semester</th>
			<th field="NamaJenisMK" width="75">Jenis MK</th>
			<th field="NamaKelompokMK" width="160">Kelompok MK</th>
			<th field="ditawarkan" width="160">Aktif</th>
			<th field="action" align="center" formatter="actionbutton" width="170">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div>
		<table width="700" border="0">
		  <tr>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="s_kd_fakultas" style="width:250px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Kurikulum</td>
			<td>: 
				<select id="s_kd_kurikulum" name="s_kd_kurikulum" style="width:230px;">
					
				</select>
			</td>
		  </tr>
		  <tr>
			<td>Program Studi</td>
			<td>: 
				<select id="s_kd_prodi" name="s_kd_prodi" style="width:250px;">
					
				</select>
			</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton" iconCls="icon-search">Filter</a></td>
		  </tr>
		</table>
	</div>
</div>
	<!-- AREA untuk Form Add/EDIT SAP >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  -->
	<div id="dialog-sap" class="easyui-dialog" style="width:400px;height:150px" closed="true" buttons="#dlg-buttons-sap">
		
	</div>
	<div id="dlg-buttons-sap">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="save_sap()">Save</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-sap').dialog('close')">Cancel</a>
	</div>
	
<!-- FORM IMPORT -->
<div id="dlgimport_mk" class="easyui-dialog" style="width:500px;height:200px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
	<div class="ftitle">Import Data Mahasiswa dari Excel</div>
	<?php 
		$attributes = array('id' => 'fmimport_mk');
		echo form_open_multipart('', $attributes);
	?>
		<div class="fitem">
		<label style="width:100px">Fakultas</label>
			<select id="kd_fakultas2" name="kd_fakultas" style="width:200px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
			</select>
		</div>	
				
		<div class="fitem" >
				<label style="width:100px">Prodi</label>
				<select id="kd_prodi2" name="kd_prodi" style="width:200px;">
				</select>
		</div>	
		<div class="fitem">
			<label style="width:100px;vertical-align:top">File</label>
			<input type="file" name="datafile"/>
		</div>
	</form>
	<div id="dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="importData()">Upload</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgimport_mk').dialog('close')">Cancel</a>
	</div>
</div>